package com.ljfiction.book.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ljfiction.book.Baner.AddBookBean;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.BaseActivity;
import com.ljfiction.book.adapter.PlotLableAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.ljfiction.book.tools.Tool.showToast;

/**
 * Created by Administrator on 2017/7/24.
 */

public class Booktabledialog {
    private Listener listener;
    private Context context;
    private List<AddBookBean.DataBean.BooklablesListBean> plotLableList=new ArrayList<>();
    private Dialog mDialog;
    private ImageView backimageView;
  private RecyclerView tableGridView;
    private TextView textViewok;
    private PlotLableAdapter plotLableAdapter;

    public Booktabledialog(BaseActivity context, List<AddBookBean.DataBean.BooklablesListBean> plotLableList, Listener listener){
        this.context=context;
        this.listener=listener;
        this.plotLableList=plotLableList;
        mDialog = new Dialog(context, R.style.AppTheme3);
        mDialog.setContentView(R.layout.booktable_dialog);
        mDialog.getWindow().setGravity(Gravity.BOTTOM);
        mDialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        mDialog.setCanceledOnTouchOutside(true);
        backimageView= (ImageView) mDialog.findViewById(R.id.back_booktable);
        textViewok= (TextView) mDialog.findViewById(R.id.ok_booktable);
        tableGridView= (RecyclerView) mDialog.findViewById(R.id.gridView_booktable);
        mDialog.show();
        intData();
    }

    private void intData() {
        backimageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
        textViewok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ss = "";
                String id="";
                int n=0;
                if (listener != null) {
                    for (int i = 0, size = plotLableList.size(); i < size; i++) {
                        if (plotLableList.get(i).isCleck()) {
                            ss += plotLableList.get(i).getBookLableName()+",";
                            id+=plotLableList.get(i).getId()+",";
                            n++;
                        }
                    }
                    if("".equals(ss)||n<2){
                        showToast("请选择最少2个标签",context);
                    }else {
                        listener.ok(ss.substring(0, ss.length() - 1),id.substring(0,id.length()-1));
                        close();
                    }

                }
            }
        });
        plotLableAdapter =new PlotLableAdapter(R.layout.table_item,plotLableList);
        tableGridView.setAdapter(plotLableAdapter);
        tableGridView.setLayoutManager(new GridLayoutManager(context,5));
        plotLableAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (plotLableList.get(position).isCleck()){
                    plotLableList.get(position).setCleck(false);
                }else {
                    int n=0;
                    for (int j = 0,size=plotLableList.size(); j <size ; j++) {
                        if (plotLableList.get(j).isCleck()){
                            n++;
                            if (n>=5){
                                showToast("最多只能选择5个标签",context);
                                return;
                            }
                        }
                    }
                    plotLableList.get(position).setCleck(true);
                }
                plotLableAdapter.notifyDataSetChanged();
            }
        });
        plotLableAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (plotLableList.get(position).isCleck()){
                    plotLableList.get(position).setCleck(false);
                }else {
                    int n=0;
                    for (int j = 0,size=plotLableList.size(); j <size ; j++) {
                        if (plotLableList.get(j).isCleck()){
                            n++;
                            if (n>=4){
                                showToast("最多只能选择5个标签",context);
                                return;
                            }
                        }
                    }
                    plotLableList.get(position).setCleck(true);
                }
                plotLableAdapter.notifyDataSetChanged();
            }
        });

    }
    public Dialog show(){
        if (mDialog!=null) {
            mDialog.show();
        }
        return mDialog;
    }
    public void close(){
        if (mDialog!=null) {
            mDialog.dismiss();
        }
    }
    public static interface Listener{
        public void ok(String str,String id);
    }
}
